<template>
<div class="login">
  
  <div class="login-container">
    <div class="login-header">
      <div class="brand">
          <b>一言律师事务所</b>
      </div>
    </div>

    <el-form :model="modelRef" :rules="rulesRef" ref="formRef">
      <el-form-item label="" prop="UserName">
        <el-input v-model="modelRef.UserName" placeholder="用户名"></el-input>
      </el-form-item>
      <el-form-item label="" prop="Password">
        <el-input v-model="modelRef.Password" type="password" placeholder="密码" @keydown.enter="login"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="login" v-loading.fullscreen.lock="loading" style="width:100%">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</div>
</template>

<script lang="ts">
  import { defineComponent, onMounted, ref, reactive, watch, Ref } from "vue";
  import { useRouter } from 'vue-router';
  import { ElForm } from 'element-plus';
  import { ResponseData } from '@/utils/http-client';
  import { Login_Do, LoginRequest } from '@/services/login';
  import { setInfo } from '@/utils/token';
  export default defineComponent({
    name: 'Login',
    components: {
     
    },
    setup() {
      const router = useRouter();
      //const { currentRoute } = router;
      const formRef = ref<typeof ElForm>();
      // 表单值
      const modelRef = reactive<LoginRequest>({
          UserName: '',
          Password: ''
      });
      // 表单验证
      const rulesRef = reactive({
          UserName: [
              { type: "string",required: true, message: '请输入用户名', trigger: 'blur' },
              { type: "string",min: 2, max: 10, message: '长度在 2 到 20 个字符', trigger: 'blur' }
          ],
          Password: [
              { type: "string",required: true, message: '请输入密码', trigger: 'blur' },
              { type: "string",min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
          ],            
      });

      const loading = ref<boolean>(false);
       
      const login = async () => {        
        loading.value = true
        formRef.value?.validate(function(valid:boolean, obj:any){
          if(!valid){
            loading.value = false
            return
          }
          
          Login_Do(modelRef).then((response: ResponseData)=>{
            if(response.Success){
              setInfo(response.Data).then(()=>{
                router.push({name:'Home'});
              })
            }else{
              loading.value = false
            }
          })
        })
        //const response: ResponseData = await Login_Do(this.form)
      }
      return {modelRef,rulesRef,formRef,loading, login}
    },
  });
 
</script>
<style scoped lang="scss">
form {
  padding: 0;
  color: #bababa;
  width: 400px;
  margin: 0 auto;
}
.login{
  height: 100vh;
  width: 100%;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -ms-flex-align: center;
  align-items: center;
}
.login-container{
    -webkit-box-flex: 1;
    -ms-flex: 1;
    flex: 1;
}
.login-header{
  padding-bottom: 10px;
}
.brand{
  font-size: 20px;
}
</style>